package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.conn.GetTravelReport;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilTime;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

/**
 * Created by yzf on 2016/2/17.
 */
public class TravelReportActivity extends JTActivity {

    private TextView title_name;

    private TextView travel_time, fuel, price,avg_fuel, mileage, maxspeed, avgspeed, speedUp, speedDown;

    private TextView start1, end1, start2, end2, start_time1, end_time1, start_time2, end_time2, mileage1, fuel1;

    private TextView travel_data_time;

    private TextView textRight;

    private TextView average_speed,sharp_turn,fatigue_driving,speeding;

    private String travel_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_travel_report);

        init_view();

        travel_id = getIntent().getStringExtra("id");

        new GetTravelReport(travel_id, new AsyCallBack<GetTravelReport.Info>() {

            private UtilTime time = new UtilTime() {

                @Override
                public void onConversion(String d, String s, String s1, String s2) {

                    travel_time.setText(s + "小时" + s1+"分");

                }

            };

            @Override
            public void onSuccess(String toast, int type, GetTravelReport.Info info) throws Exception {

                if (info.msg.equals("000")) {

//                    Float time = (Float.parseFloat(info.endTime) - Float.parseFloat(info.startTime));

                    time.loadMillisecond(UtilTime.HOUR_MINUTE_SECOND, Long.parseLong(info.endTime) - Long.parseLong((info.startTime)));

                    fuel.setText(new DecimalFormat("##0.00").format(Float.parseFloat(info.fuel) / 1000) + "升");

                    price.setText("约"+Tools.F_num(String.valueOf(Double.parseDouble(info.fuel)/1000*Double.parseDouble(info.price)))+"元");

                    if (info.fuel.equals("0.0")) {

                        avg_fuel.setText("0.0" + "升/公里");
                    } else if(Tools.F_num(info.mileage).equals("0")){

                        avg_fuel.setText("行驶里程为0，无法计算");

                    } else{

                        avg_fuel.setText(new DecimalFormat("##0.00").format(((Float.parseFloat(info.fuel) / 1000) / (Float.parseFloat(info.mileage) / 1000))*100)  + "升/百公里");
                    }

                    mileage.setText(new DecimalFormat("##0.00").format(Float.parseFloat(info.mileage) / 1000) + "公里");

                    maxspeed.setText(Tools.F_num(info.maxspeed) + "公里/小时");

                    avgspeed.setText(Tools.F_num(info.avgspeed) + "公里/小时");

                    speedUp.setText(info.speedUp + "次");

                    speedDown.setText(info.speedDown + "次");

                    average_speed.setText(info.avgengine + "转/分");

                    sharp_turn.setText(info.turn_count + "次");

                    speeding.setText(info.highspeedCount + "次");

                    if(info.driver_flag.equals("1")){

                        fatigue_driving.setText("有");

                    }else if(info.driver_flag.equals("0")){

                        fatigue_driving.setText("无");

                    }

                    //2
                    start1.setText(info.startName);
                    start2.setText(info.startName);

                    end1.setText(info.endName);
                    end2.setText(info.endName);

                    start_time1.setText(new SimpleDateFormat("HH:mm").format(Long.parseLong(info.startTime)));
                    start_time2.setText(new SimpleDateFormat("HH:mm").format(Long.parseLong(info.startTime)));

                    end_time1.setText(new SimpleDateFormat("HH:mm").format(Long.parseLong(info.endTime)));
                    end_time2.setText(new SimpleDateFormat("HH:mm").format(Long.parseLong(info.endTime)));

                    mileage1.setText(new DecimalFormat("##0.00").format(Float.parseFloat(info.mileage) / 1000) + "公里");
                    fuel1.setText(new DecimalFormat("##0.00").format(Float.parseFloat(info.fuel) / 1000) + "升");

                } else {

                    Toast.makeText(context, info.msg, Toast.LENGTH_SHORT).show();
                }
            }
        }).execute(this);
    }

    private void init_view() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("行程报告");

        (textRight = (TextView) findViewById(R.id.commit)).setText("查看轨迹");

        travel_time = (TextView) findViewById(R.id.travel_time);

        fuel = (TextView) findViewById(R.id.fuel);

        price = (TextView) findViewById(R.id.price);

        avg_fuel = (TextView) findViewById(R.id.avg_fuel);

        mileage = (TextView) findViewById(R.id.mileage);

        maxspeed = (TextView) findViewById(R.id.maxspeed);

        avgspeed = (TextView) findViewById(R.id.avgspeed);

        speedUp = (TextView) findViewById(R.id.speedUp);

        speedDown = (TextView) findViewById(R.id.speedDown);

        start1 = (TextView) findViewById(R.id.start1);

        end1 = (TextView) findViewById(R.id.end1);

        start_time1 = (TextView) findViewById(R.id.start_time1);

        end_time1 = (TextView) findViewById(R.id.end_time1);

        start2 = (TextView) findViewById(R.id.start2);

        end2 = (TextView) findViewById(R.id.end2);

        start_time2 = (TextView) findViewById(R.id.start_time2);

        end_time2 = (TextView) findViewById(R.id.end_time2);

        mileage1 = (TextView) findViewById(R.id.mileage1);

        fuel1 = (TextView) findViewById(R.id.fuel1);

        average_speed = (TextView) findViewById(R.id.average_speed);

        sharp_turn = (TextView) findViewById(R.id.sharp_turn);

        fatigue_driving = (TextView) findViewById(R.id.fatigue_driving);

        speeding = (TextView) findViewById(R.id.speeding);


    }

    @Override
    public void onRightClick(View v) {

        super.onRightClick(v);

            startActivity(new Intent(context, TrackActivity.class)
                    .putExtra("id", getIntent().getStringExtra("id")));

            finish();


    }
}
